﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Data;
using CSNN.GUI;

namespace CSNN
{
   /// <summary>
    /// Логика взаимодействия для WindowNeuralNetCreate.xaml
    /// </summary>
    public partial class WindowNeuralNetCreate : Window
    {
        private BindingList<NeuronsInLayer> NeuronsCountList;
        private NeuralNetwork net;
        private VoidDelegat UpdateMainWindow;

        public WindowNeuralNetCreate()
        {
            InitializeComponent();
        }

        public void SetNet(NeuralNetwork N, VoidDelegat dl)
        {
            UpdateMainWindow = dl;
            this.net = N;
        }

        /// <summary>
        /// Событие кнопки начала заполнения количества нейронов в слоях
        /// </summary>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            DataGridNeuronsCount.Columns.Clear();

            NeuronsCountList = new BindingList<NeuronsInLayer>();

            for (int i = 0; i < Int32.Parse(this.LabelLayerCount.Text); i++)
            {
                NeuronsCountList.Add(new NeuronsInLayer(1));
            }

            DataGridNeuronsCount.ItemsSource = NeuronsCountList;
            DataGridNeuronsCount.IsReadOnly = false;

            button2.IsEnabled = true;
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Установка сети начальными значениями
        /// </summary>
        private void Create_Net(object sender, RoutedEventArgs e)
        {
            int inputDem = 0;
            if (!Int32.TryParse(this.textBoxInputsCount.Text, out inputDem))
            {
                MessageBox.Show("Не корректное заполнения поля \"Количество входов\"!", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            else if (inputDem <= 0)
            {
                MessageBox.Show("Значение поля \"Количество входов\" должно быть больше нуля!", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            int Layers = Int32.Parse(this.LabelLayerCount.Text);
            int[] NeuronsCount = new int[Layers];
            int InputDemension = Int32.Parse(this.textBoxInputsCount.Text);

            for (int i = 0; i < Layers; i++)
            {
                NeuronsCount[i] = NeuronsCountList[i].Neurons;
            }

            if ((bool)radioBattonRandWeight.IsChecked)
            {
                try
                {
                    net.SetRandomSegment(Int32.Parse(textBoxRandMin.Text), Int32.Parse(textBoxRandMax.Text));
                }
                catch (ArgumentException ae)
                {
                    MessageBox.Show(ae.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }

            if ((bool)radioButtonUnit.IsChecked)
            {
                net.SetDoubleUnitRandomSegment();
            }

            this.net.SetNeuralNetwork(Layers, NeuronsCount, InputDemension);
            UpdateMainWindow();
            this.Close();
        }

        private void radioButton1_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void SliderLayerCount_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (button2 == null)
            {
                return;
            }
            
            button2.IsEnabled = false;
        }
     

        
    }
}
